
#include "button.h"

Button::Button() : Image()
{
	hasImageOverlay = false;
}

Button::~Button()
{

}

bool Button::initialize(Graphics *g, Input *i, Audio *a, int width, int height, int ncols,
							TextureManager *textureM)
{
	spriteData.width = buttonNS::WIDTH;           
	spriteData.height = buttonNS::HEIGHT;
	startFrame = buttonNS::START_FRAME;      
	endFrame     = buttonNS::END_FRAME; 
	spriteData.rect.bottom = buttonNS::HEIGHT;    
	spriteData.rect.right = buttonNS::WIDTH;
	currentFrame = 0;
	cols = 2;
	
	input = i;                // the input system
	audio = a;                // the audio system
	return(Image::initialize(g, width, height, ncols, textureM));
}

bool Button::isHovered()
{
	bool result = true;
	if (getX()> input->getMouseX())
		result = false;
	if (getX() + getWidth() < input->getMouseX())
		result = false;
	if (getY() > input->getMouseY())
		result = false;
	if (getY() + getHeight() < input->getMouseY())
		result = false;
	return result;
}

bool Button::isClicked()
{
	return (isHovered() && input->getMouseLButton());
}

void Button::update(float frameTime)
{
	if (isHovered())
		setCurrentFrame(1);
	else
		setCurrentFrame(0);
	if (isClicked())
		setCurrentFrame(2);	
}

void Button::setImageOverlay(Image overlay)
{	
	imageOverlay = overlay;
	imageOverlay.setX(getX() + getWidth()/2 - imageOverlay.getWidth()/2);
	imageOverlay.setY(getY() + getHeight()/2 - imageOverlay.getHeight()/2);
	hasImageOverlay = true;
}

void Button::draw(COLOR_ARGB color)
{
	Image::draw(color);
	if (hasImageOverlay)
		imageOverlay.draw(color);	
}

void Button::setX(float x)
{
	if (hasImageOverlay)
		imageOverlay.setX(getX() + getWidth()/2 - imageOverlay.getWidth()/2);
	Image::setX(x);
}

void Button::setY(float y)
{
	if (hasImageOverlay)
		imageOverlay.setY(getY() + getHeight()/2 - imageOverlay.getHeight()/2);
	Image::setY(y);
}